//
//  TPArticleDetailViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/30.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPArticleDetailViewController.h"
#import "YPWebViewModel.h"
#import "TPArticleDetailCell.h"
#import "TPArticleDetailHeader.h"
#import "TPArticleDetailFooter.h"
#import "TPArticleCommentModel.h"
@interface TPArticleDetailViewController ()<WKUIDelegate,WKNavigationDelegate>
@property (nonatomic,strong)WKWebView *webView;
@property (nonatomic,strong)TPArticleDetailFooter *footer;
@end

@implementation TPArticleDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"内容详情";
    [self setup];
    
    self.model.article_view ++;
}
-(void)setup{
    
    
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50.0, 0);
    
    _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 50.0)];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    self.tableView.tableHeaderView = _webView;
//    [_webView loadRequest:[NSURLRequest requestWithURL:urlNamed(@"http://api.ddlemon.com/yisheng/jiguang/76.html")]];
    
    NSString *url = [NSString stringWithFormat:@"http://api.52charming.com/api/article/detail?article_id=%ld",self.model.article_id];
    [_webView loadRequest:[NSURLRequest requestWithURL:urlNamed(url)]];
    
//    if(self.model.article_url.length > 0)
//   [_webView loadRequest:[NSURLRequest requestWithURL:urlNamed(self.model.article_url)]];
//    else{
//        [_webView loadHTMLString:xw_getNULLString(self.model.article_content) baseURL:nil];
//    }
    _footer = [[TPArticleDetailFooter alloc]init];
    [self.view addSubview:_footer];
    [_footer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0.0);
        make.height.mas_equalTo(50.0);
    }];
    
     @weakify(self);
    [_footer setDidBlock:^(UIButton *sender, UITextField *textField) {
        @strongify(self);
        [self send:textField.text];
        textField.text = @"";
    }];
    
    
    [self getRequest];
    [self.tableView xw_addPullToRefreshWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page = 1;
        [self getRequest];
        
    }];
    
    
    [self.tableView xw_addInfinityScrollWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page ++;
        [self getRequest];
    }];
}

-(void)getRequest{
    
    [[TPRequestClient sharedClient]getArticleDetail_current:self.page size:kMaxRequestSize article_id:self.model.article_id success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        if(self.page == 1)
            [self.dataSource removeAllObjects];
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPArticleCommentModel class] json:respone.data[@"data"]];
        [self.dataSource addObjectsFromArray:array];
        [self refreshSccuess];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        [self refreshFail];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [self refreshFail];
    }];
    
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    @weakify(self);
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id data, NSError * _Nullable error) {
        
        @strongify(self);
        CGFloat height = [data floatValue];
        self.webView.height = height + 40.0f;
        self.tableView.tableHeaderView = self.webView;
        DLog(@"=-------didFinishNavigation ------%@",data);
    }];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TPArticleDetailCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellx"];
    if(!cell){
        cell = [[TPArticleDetailCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellx"];
    }
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TPArticleDetailHeader *header = [[TPArticleDetailHeader alloc]initWithFrame:CGRectMake(0, 0, tableView.width, 60.0)];
    return header;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

-(void)send:(NSString *)text{
    
    [self.view endEditing:YES];
    
    if(!xw_getLogin()){
        return;
    }
    
    if(text.length == 0){
        [self.view xw_hideTitle:@"请输入内容"];
        return;
    }
    
    [[TPRequestClient sharedClient]getArticleReplyId:self.model.article_id content:text success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        [self.view xw_hideTitle:@"评论成功"];
        [self getRequest];
        
        ++self.model.article_pl;
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
        [self.view xw_hideTitle:respone.message];
        
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self.view xw_hideTitle:error.localizedDescription];
    }];
}

@end
